package com.txzt.gaea.archive.controller;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.archive.dto.BizArchiveDTO;
import com.txzt.gaea.archive.model.BizArchive;
import com.txzt.gaea.archive.service.BizArchiveService;
import com.txzt.gaea.archive.vo.BizArchiveRecordVo;
import com.txzt.gaea.archive.vo.BizArchiveVo;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.page.PageInfoBT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/biz/archive")
public class BizArchiveController extends BaseController {
    @Autowired
    private BizArchiveService bizArchiveService;


    /**
     * 档案统计
     * @return
     */
    @GetMapping(value = "/statistics")
    public RtnInfo<?> statistics() {
        return RtnInfo.success(bizArchiveService.statisticsBizArchive());
    }

    @PostMapping(value = "/list")
    public RtnInfo<PageInfoBT<BizArchiveVo>> pageList(@RequestBody BizArchiveDTO bizArchiveDTO){
        return bizArchiveService.pageList(bizArchiveDTO);
    }

    @PostMapping(value = "/record/list")
    public RtnInfo<PageInfoBT<BizArchiveRecordVo>> pageRecordList(@RequestBody BizArchiveDTO bizArchiveDTO){
        return bizArchiveService.pageRecordList(bizArchiveDTO);
    }

    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo add(@RequestBody BizArchive bizArchive){
        if (null != bizArchive && null == bizArchive.getId()){
            //查重
            if (!bizArchiveService.addCheckName(bizArchive)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查档案名称");
            }
            int num = bizArchiveService.save(bizArchive);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据添加失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/item/{id}")
    public RtnInfo getInfoById(@PathVariable Long id){
        return bizArchiveService.getInfo(id);
    }

    @PostMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo edit(@RequestBody BizArchive bizArchive){
        if (null != bizArchive && null != bizArchive.getId()){
            //查重
            if (!bizArchiveService.updateCheckName(bizArchive)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查档案名称");
            }
            int num = bizArchiveService.update(bizArchive);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据更新失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/delete/{id}")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo remove(@PathVariable Long id){
        return RtnInfo.success(bizArchiveService.delete(id));
    }

    @PostMapping(value = "/borrow")
    public RtnInfo borrowArchive(@RequestBody BizArchiveDTO param){
        return RtnInfo.success(bizArchiveService.borrowArchive(param));
    }

    @PostMapping(value = "/return")
    public RtnInfo returnArchive(@RequestBody BizArchiveDTO param){
        return RtnInfo.success(bizArchiveService.returnArchive(param));
    }
}
